package com.imddysc.jzknavigator.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.imddysc.jzknavigator.service.ZknavigatorService;
import com.imddysc.jzknavigator.utils.PathNode;
import com.imddysc.jzknavigator.utils.ZknaviUtils;

@Controller
public class ZknavigatorIndexController {
	
	private static final Logger logger = LoggerFactory.getLogger(ZknavigatorIndexController.class);
	
	@Autowired
	private ZknavigatorService zknavigatorService;
	
	@RequestMapping(value = "/{path}")
	public ModelAndView index(@PathVariable("path") String path) {
		ModelAndView mav = new ModelAndView();
		
		logger.info("index: " + path);
		Map pahtMap = ZknaviUtils.pathHref(path);
		logger.info("" + ((ArrayList)pahtMap.get("pathAHrefList")).size());
		logger.info(Arrays.toString((Object[])pahtMap.get("dstrArray")));
		logger.info(Arrays.toString((Object[])pahtMap.get("fstrArray")));
		mav.addObject("dstrArray",Arrays.asList((Object[])pahtMap.get("dstrArray")));
		mav.addObject("fstrArray",Arrays.asList((Object[])pahtMap.get("fstrArray")));
		mav.addObject("pathAHrefList",pahtMap.get("pathAHrefList"));
		mav.addObject("pathUrl",pahtMap.get("pathUrl"));
		
		
		String path1 = path.replace("|","/");
		String path2 = path1.replace("//","/");
		logger.info("path: " + path);
		logger.info("path1: " + path1);
		logger.info("path2: " + path2);
		List<PathNode> pathNodeList = null;
		try {
			pathNodeList = zknavigatorService.getPathNodesForPath(path2);
			logger.info("pathNodeList: " + pathNodeList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mav.addObject("pathNodeList", pathNodeList);
		
		
		mav.setViewName("index");
		return mav;
	}
	

	@RequestMapping(value = "/")
	public ModelAndView index0() {
		String path = "|";
		logger.info("index: " + path);
		return index(path);
	}
	
	@ResponseBody
	@RequestMapping(value = "/favicon.ico")
	public String favicon() {
		String path = "favicon.ico";
		logger.info("favicon.ico: ");
		return "";
	}

}
